<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div id="divbox">
    <div class="login-form">
      <div id="logo" style="padding-top: 30px">
        <span class="login-title">欢迎登录考勤管理系统</span>
      </div>
      <el-form
        class="el-form"
        :model="loginform"
        :rules="loginRule"
        ref="loginformRef"
        label-width="20%"
      >
        <el-form-item class="label" label="账&emsp;号" prop="email">
          <el-input v-model="loginform.email" class="input"></el-input>
        </el-form-item>
        <el-form-item class="label" label="密&emsp;码" prop="password">
          <el-input type="password" v-model="loginform.password" class="input"></el-input>
        </el-form-item>
        <br />
        <div style="display:flex justify-content:center">
          <el-button
            type="primary"
            @click="onSubmit"
            :disabled="isCanClick"
            style="width: 80px"
            >登录</el-button
          >
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup scoped>
import { computed, reactive, ref } from "vue";
import { ElMessage } from "element-plus"; //信息提示相关库
import router from "@/router";
import { setToken } from "@/utils/auth";
import { login } from "../http/api/user.js";

let loginform = reactive({
  email: "",
  password: "",
});

//登录表单格式校验
let loginRule = {
  email: [
    { required: true, message: "邮箱选项为必填项", trigger: "blur" },
    {
      pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
      message: "请输入正确的邮箱格式",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "密码选项为必填项", trigger: "blur" },
    { min: 8, message: "密码必须长度必须大于8位", trigger: "blur" },
    {
      pattern: /(?![A-Z]*$)(?![a-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)/,
      message: "密码必须包含大写字母、小写字母、数字、特殊符号中的至少两种",
      trigger: "blur",
    },
  ],
};

//邮箱和密码均不为空，才能点击"登录"按钮
let isCanClick = computed(() => {
  const { email, password } = loginform;
  return Boolean(!(email && password));
});

const loginformRef = ref();

//  登录
const onSubmit = () => {
  //1.数据格式验证
  loginformRef.value.validate((valid: boolean) => {
    if (!valid) {
      ElMessage.error("登录邮箱或密码填写错误,请仔细检查！");
      return;
    } else {
      //2.数据向后端发送进行验证
      login(JSON.stringify(loginform)).then(
        (res) => {
          if (res.code == 200) {
            const token = res.data.token;
            setToken(token);
            ElMessage({
              showClose: true,
              message: "恭喜你，登录成功✔",
              type: "success",
            });
            router.push("/am_system");
          } else {
            ElMessage({
              showClose: true,
              message: res.response.data.message,
              type: "error",
            });
          }
        },
        (error) => {
          {
            //处理异常
            console.log(error);
          }
        }
      );
    }
  });
};
</script>

<style scoped>
#divbox {
  width: 100%;
  height: 100vh;
  background-image: url("../assets/bk.png"); /*将页面背景  图片路径替换为你自己的图片路径*/
  background-size: cover; /*保持图片比例并完全覆盖元素*/
  background-position: center center; /*将图片居中对齐*/
}

.login-form {
  width: 24%;
  height: 40%;
  color: #fff;
  background-color: rgba(9, 37, 73, 0.913);
  position: absolute;
  left: 38%;
  top: 30%;
  translate: (-50%, -50%);
}
.login-title {
  color: rgb(239, 238, 235);
}
#logo {
  width: 100%;
  height: 60px;
  /* background-color: aquamarine; */
  margin-top: 10px;
  margin-bottom: 55px;
  font-size: 32px;
}

.input {
  max-width: 300px;
}

img {
  border-radius: 50%;
  width: 100px;
  position: absolute;
  left: 638px;
  top: 50px;
}

.el-form-item__label {
  color: #fff;
}
</style>
